from django.contrib.auth.decorators import login_required, user_passes_test

from django.shortcuts import get_object_or_404
from django.http import HttpResponse

from assets.models import *
from assets.util import HttpResponseSerialized, Fields


@login_required()
def collections(request):  
  collections = UserCollection.objects.filter(user=request.user)
  return HttpResponseSerialized(request, collections, fields=('name', 'stamp'))

  
@login_required() 
def create_collection(request, name):
  if UserCollection.objects.filter(user=request.user, name=name).count() > 0:
    return HttpResponse('Conflict: a UserCollection with name %s already exists!'%(name), status=409)
  collection = UserCollection.objects.create(user=request.user, name=name)
  collection.save()
  return HttpResponseSerialized(request, collection, fields=('name', 'assets', 'stamp'))  
 

@login_required()   
def delete_collection(request, name):
  collection = get_object_or_404(UserCollection, user=request.user, name=name)
  collection.delete()
  return HttpResponse('Successfully deleted %s'%(name), status=200)
 

@login_required() 
def rename_collection(request, name, newName):
  if UserCollection.objects.filter(user=request.user, name=newName).count() > 0:
    return HttpResponse('Conflict: a UserCollection with name %s already exists, failed to rename %s !'%(newName, name), status=409)
  collection = get_object_or_404(UserCollection, user=request.user, name=name)
  collection.name = newName
  collection.save()
  return HttpResponse('Successfully renamed %s to %s'%(name, newName), status=200)


@login_required()
def collection(request, name):  
  collection = get_object_or_404(UserCollection, user=request.user, name=name)
  return HttpResponseSerialized(request, collection, fields=('name', 'assets', 'stamp'))  

  
@login_required()  
def collection_add_asset(request, name, as_id):
  collection = get_object_or_404(UserCollection, user=request.user, name=name)
  asset = get_object_or_404(Asset, id=as_id)
  collection.assets.add(asset)
  collection.save() #updates the stamp!
  return HttpResponse('Successfully added %s to %s'%(as_id, name), status=200)


@login_required()  
def collection_remove_asset(request, name, as_id):
  collection = get_object_or_404(UserCollection, user=request.user, name=name)
  asset = get_object_or_404(Asset, id=as_id)
  collection.assets.remove(asset)
  collection.save() #updates the stamp!
  return HttpResponse('Successfully removed %s to %s'%(as_id, name), status=200)
  
  
@user_passes_test(lambda u: u.is_superuser)  
def users_collections(request):
  collections = UserCollection.objects.all()
  start = request.GET.get('start', 0)
  end = request.GET.get('end', None)
  since =  request.GET.get('since', None)
  if since:
    collections.filter(stamp__gte=since)
    collections = collections.order_by('stamp')
  collections = collections[start:end]
  users = User.objects.filter(id__in=list(collections.values_list('user', flat=True)))
  return HttpResponseSerialized(request, list(collections)+list(users), fields=('user', 'name', 'assets', 'username', 'stamp')) 
  
  
  
  
  
  
